﻿using System;
using System.Web.UI.WebControls;
using METU_CMT.Business;

namespace METU_CMT
{
    public partial class EditStudentsScreen : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStudentsHaveAccounts();
                BindStudentsOfCurrentCourse();
            }
        }

        protected void OnBtnAddClicked(object sender, EventArgs e)
        {
            Instructor _instructor = (Instructor)EntityFactory.User;

            bool _successfull;
            DateTime _expiryDate = CommonMethods.ToDateTime(txtNewStudentExpiryDate.Text, "dd.MM.yyyy", out _successfull);

            if(_successfull == false)
            {
                return;
            }

            string _password = CommonMethods.CreateRandomPassword(6);

            //TODO: Must send an email for giving password to student.
            _instructor.AddNewUser(txtNewStudentName.Text, txtNewStudentSurName.Text, txtNewStudentUserName.Text,
                                   _password, txtNewStudentEmail.Text, UserType.Student, _expiryDate);

            BindStudentsHaveAccounts();
        }

        private void BindStudentsHaveAccounts()
        {
            dtgStudentsHavingAccounts.DataSource = EntityFactory.GetAllStudents();
            dtgStudentsHavingAccounts.DataBind();
        }

        protected void OnBtnPullFromMetuOnlineClicked(object sender, EventArgs e)
        {

        }

        private void BindStudentsOfCurrentCourse()
        {
            dtgStudents.DataSource = EntityFactory.GetStudentsOfCourse(EntityFactory.CourseId);
            dtgStudents.DataBind();
        }

        protected void OnRowDtgStudentsHavingAccountsRowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.CompareTo("AddToCourse") == 0)
            {
                int _studentId;
                if (int.TryParse(e.CommandArgument.ToString(), out _studentId) == false)
                {
                    //TODO: Log student id alınamadı.
                }
                Instructor _instructor = (Instructor)EntityFactory.User;
                _instructor.AssignStudentToCourse(EntityFactory.CourseId, _studentId);

                BindStudentsOfCurrentCourse();
            }
        }

        protected void OnDtgStudentsCancellingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            dtgStudents.EditIndex = -1;
            BindStudentsOfCurrentCourse();
        }

        protected void OnDtgStudentsRowEditing(object sender, GridViewEditEventArgs e)
        {
            dtgStudents.EditIndex = e.NewEditIndex;
            BindStudentsOfCurrentCourse();
        }

        protected void OnDtgStudentsRowCommand(object sender, GridViewCommandEventArgs e)
        {

        }

        protected void OnDtgStudentsRowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            dtgStudents.Rows[e.RowIndex].Focus();
            int _studentId = CommonMethods.GetIdFromGridViewRow(dtgStudents.Rows[e.RowIndex]);

            TextBox _txtNameEdit = (TextBox)dtgStudents.Rows[e.RowIndex].FindControl("txtNameEdit");
            TextBox _txtSurnameEdit = (TextBox)dtgStudents.Rows[e.RowIndex].FindControl("txtSurnameEdit");
            TextBox _txtUserNameEdit = (TextBox)dtgStudents.Rows[e.RowIndex].FindControl("txtUserNameEdit");
            TextBox _txtEMailEdit = (TextBox)dtgStudents.Rows[e.RowIndex].FindControl("txtEMailEdit");
            TextBox _txtExpiryDateEdit = (TextBox)dtgStudents.Rows[e.RowIndex].FindControl("txtExpiryDateEdit");

            bool _successfull;
            DateTime _expiryDate = CommonMethods.ToDateTime(_txtExpiryDateEdit.Text, "dd.MM.yyyy", out _successfull);

            if (_successfull == false)
            {
                return;
            }

            Instructor _instructor = (Instructor)EntityFactory.User;
            _instructor.UpdateUser(_studentId, _txtNameEdit.Text, _txtSurnameEdit.Text, _txtUserNameEdit.Text, _txtEMailEdit.Text,
                                   _expiryDate);

            dtgStudents.EditIndex = -1;

            BindStudentsOfCurrentCourse();
            BindStudentsHaveAccounts();
        }

        protected void OnDtgStudentsRowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int _studentId = CommonMethods.GetIdFromGridViewRow(dtgStudents.Rows[e.RowIndex]);
            Instructor _instructor = (Instructor)EntityFactory.User;
            _instructor.DeleteStudentFromCourse(EntityFactory.CourseId, _studentId);

            BindStudentsOfCurrentCourse();
        }
    }
}